<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Exceptions\MySQLException;

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;

    protected function respondSuccess($data)
    {
        if (request()->wantsJson()) {
            return response()->json([
                'code' => 20000,
                'success' => 'ok',
                'data' => $data,
            ]);
        }
        return response()->noContent();
    }

    protected function respondWithToken($token)
    {
        return response()->json([
            'code' => 20000,
            'success' => 'ok',
            'data' => [
                'access_token' => $token,
                'token_type'   => 'bearer',
            ],
        ]);
    }

    protected function respondError($message)
    {
        if (request()->wantsJson()) {
            // throw new MySQLException($message ?? HttpCodeEnum::getDescription(HttpCodeEnum::SYSTEM_ERROR));
            return response()->json([
                'code' => 50000,
                'success' => 'ok',
                'data' => $message,
            ]);
        }

        return redirect()->back()->withErrors($message)->withInput();
    }
}
